#
# Main Makefile
# from Makefile template for multi-directory
#

# object file directory, eg
# $ make O=../build
# $ export BUILD_DIR=../build ; make
# or modify BUILD_DIR here

BUILD_DIR=obj

ifdef O
ifeq ("$(origin O)", "command line")
BUILD_DIR := $(O)
endif
endif

ifneq ($(BUILD_DIR),)
saved-output := $(BUILD_DIR)

# Attempt to create a output directory.
$(shell [ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR})

# Verify if it was successful.
BUILD_DIR := $(shell cd $(BUILD_DIR) && pwd)
$(if $(BUILD_DIR),,$(error output directory "$(saved-output)" does not exist))
endif  # ifneq ($(BUILD_DIR),)

OBJTREE   := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR))
SRCTREE   := $(CURDIR)
LNDIR     := $(OBJTREE)
export        SRCTREE OBJTREE

include $(SRCTREE)/config.mk

###############################################################################
# libs
### your libs here

MGR_FW_LIB := \
	mgrfw/cmsvc/libcmsvc.a \
	mgrfw/common/libcommon.a \
	mgrfw/sysadmmgr/libsysadmmgr.a \
	mgrfw/extappmgr/libextappmgr.a \
	mgrfw/datapathmgr/libdatapathmgr.a

PF_DEP_LIB := \
	pfdep/mtk7620/libmtk7620.a

OS_DEP_LIB := \
	osdep/linux/liblinux.a

LIBS := $(MGR_FW_LIB) $(PF_DEP_LIB) $(OS_DEP_LIB)
	
LIBS := $(addprefix $(obj),$(LIBS))

EXTLIBS := \
	mgrfw/cmapi/libcmapi.a \

EXTLIBS := $(addprefix $(obj),$(EXTLIBS))

LDFLAGS += $(LIBS) -lpthread -liwpriv -lnvram

# source file(s), including c file(s) cpp file(s)
# you can also use $(wildcard *.c), etc.
SRC_C   := sysmgr.c
SRC_CPP :=

# object file(s)
OBJ_C   := $(addprefix $(obj),$(patsubst %.c,%.o,$(SRC_C)))
OBJ_CPP := $(addprefix $(obj),$(patsubst %.cpp,%.o,$(SRC_CPP)))

# executable file here
target = $(obj)sysmgr

###############################################################################

all: $(target) $(EXTLIBS) 
	@make -C test
	@make -C autoip
	@make -C nbnsd
$(target): $(LIBS) $(OBJ_C) $(OBJ_CPP)
	@echo "Generating executable file  " $(notdir $(target))
	@$(CXX) $(CFLAGS) $(OBJ_C) $(OBJ_CPP) $(LDFLAGS) -o $(target)

$(LIBS):
	@$(MAKE) --no-print-directory -C $(dir $(subst $(obj),,$@));

$(EXTLIBS):
	@$(MAKE) --no-print-directory -C $(dir $(subst $(obj),,$@))

clean:
	@echo -n "distcleaning..."
	@make -C test clean
	@make -C autoip clean
	@make -C nbnsd clean
	@$(RM) $(target)
	@find $(OBJTREE) -type f \
	        \( -name 'core' -o -name '*.bak' -o -name '*~' \
	        -o -name '*.o'  -o -name '*.a' -o -name '*.exe' -o -name '.depend' \) -print \
	        | xargs rm -f
	@$(RM) $(obj)
	@echo " done"

distclean: clean

.PHONY: all clean distclean $(LIBS) $(EXTLIBS)

romfs:
	$(ROMFSINST) $(target) /bin/sysmgr;
	$(STRIP) $(ROMFSDIR)/bin/sysmgr
#	$(ROMFSINST) autoip/autoip /bin/autoip;
#	$(STRIP) $(ROMFSDIR)/bin/autoip
#	$(ROMFSINST) nbnsd/nbnsd /bin/nbnsd;
#	$(STRIP) $(ROMFSDIR)/bin/nbnsd
	$(ROMFSINST) scripts/udhcpc_no_dns.script /sbin/udhcpc_no_dns.script
	$(ROMFSINST) scripts/udhcpc.script /sbin/udhcpc.script
	$(ROMFSINST) scripts/start_sysmgr.sh /sbin/start_sysmgr.sh

#########################################################################

include $(SRCTREE)/rules.mk

sinclude $(obj).depend

#########################################################################
